package com.gaoxizhe.ccc.controller;/**
 * @author : Mr.Gao
 * @date :   2019/9/24 22:56
 */

import com.gaoxizhe.ccc.base.RestEntityResponse;
import com.gaoxizhe.ccc.constant.ReturnBaseConstant;
import com.gaoxizhe.ccc.model.login.User;
import com.gaoxizhe.ccc.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName userController
 * @Author Mr.Gao
 * @Date 2019/9/24 22:56
 * @Description TODO |
 */
@RestController
@Slf4j
@RequestMapping(value = "/api/v1")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public ResponseEntity<RestEntityResponse<User>> register(@RequestBody User user) {
        HttpHeaders headers = new HttpHeaders();
        HttpStatus httpStatus = HttpStatus.OK;
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        RestEntityResponse<User> entityResponse = new RestEntityResponse<>();
        if (user == null || user.getEmail() == null) {
            log.info("参数错误！！:{}",user);
            entityResponse.setCode(ReturnBaseConstant.PARAMETER_ERROR);
            entityResponse.setMsg("参数错误");
            return new ResponseEntity<>(entityResponse, headers, httpStatus);
        }
        entityResponse = userService.RegisterInfo(user.getEmail(), user.getNickname(), user.getPassword(), user.getCode());
        return new ResponseEntity<>(entityResponse, headers, httpStatus);
    }

    @PostMapping("/sendCode")
    public ResponseEntity<RestEntityResponse<User>> sendPollCode(@RequestBody User user) {
        HttpHeaders headers = new HttpHeaders();
        HttpStatus httpStatus = HttpStatus.OK;
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        RestEntityResponse<User> entityResponse = new RestEntityResponse<>();
        if (user == null || user.getEmail() == null) {
            log.info("参数错误！！！:{}",user);
            entityResponse.setCode(ReturnBaseConstant.PARAMETER_ERROR);
            entityResponse.setMsg("参数错误");
            return new ResponseEntity<>(entityResponse, headers, httpStatus);
        }
        String email = user.getEmail();
        entityResponse = userService.sendCode(email);
        return new ResponseEntity<>(entityResponse, headers, httpStatus);
    }

    @PostMapping("/login")
    public ResponseEntity<RestEntityResponse<String>> testData(@RequestBody User user) {
        HttpHeaders headers = new HttpHeaders();
        HttpStatus httpStatus = HttpStatus.OK;
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        RestEntityResponse<String> entityResponse = new RestEntityResponse<>();
        if (user == null || user.getEmail() == null ||user.getPassword() == null) {
            log.info("参数错误！！！:{}",user);
            entityResponse.setCode(ReturnBaseConstant.PARAMETER_ERROR);
            entityResponse.setMsg("参数错误");
            return new ResponseEntity<>(entityResponse, headers, httpStatus);
        }
        String email = user.getEmail();
        String password = user.getPassword();

        entityResponse = userService.Login(email, password);
        return new ResponseEntity<>(entityResponse, headers, httpStatus);
    }
}
